<?php
class Application_Form_Renderer_Edit_Common extends Application_Form_Renderer_RendererAbstract
{
	
	/**
	 * Submit label, configurable by setLabel.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 * @var string $_submitLabel
	 */
	protected $_submitLabel = null;
	
	/**
	 * _setupRender() - Sets up the form before rendering. A submit button is
	 * added to the form when it is not a subform.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 */
	protected function _setupRender ()
	{
		// Add form decorators.
		$this->_form->setDecorators(array(
			'FormElements',
			array('HtmlTag', array('tag' => 'dl', 'class' => 'edit') ),
			'Form'
		));
		
		if (! $this->_isSubForm) {
			// Add a submit button.
			$this->_addSubmit();
		}
		
		// Set up the elements.
		$this->_setupElements();
	}
	
	/**
	 * _setupElements() - Set up the elements of the form.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 */
	protected function _setupElements ()
	{
		
		$elements = (array)$this->_form->getElements();
		foreach ($elements as $element) {
			$this->_setupSingleElement($element);
		}
	}
	
	/**
	 * _setupSingleElement() - Sets up the form for a single element.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 * @param Zend_Form_Element $element Element to setup.
	 */
	protected function _setupSingleElement (Zend_Form_Element $element)
	{
		parent::_setupSingleElement($element);
		
		$element
			->removeDecorator('Errors')
			->removeDecorator('Description')
			->removeDecorator('HtmlTag')
			->removeDecorator('Label')
			->addDecorator(new Application_Form_Decorator_Edit_Common());
	}
	
	/**
	 * setSubmitLabel() - Sets the label for the submit button.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 * @param string $label Submit button label.
	 * @return Application_Form_Renderer_Edit_Common Returns this for chaining.
	 */
	public function setSubmitLabel($label){
		$this->_submitLabel = $label;
		return $this;
	}
	
	/**
	 * _addSubmit() - Adds a submit button row to the form.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 */
	protected function _addSubmit ()
	{
		
		$elementSubmit = new Zend_Form_Element_Submit('submit');
		$elementSubmit->setLabel($this->_submitLabel);
		$this->_form->addElement($elementSubmit);
		$this->_form->addDisplayGroup(array('submit'), 'submit');
	}
	
}